<?php

/**
 * adminconcessions actions.
 *
 * @package    retailbrain
 * @subpackage adminconcessions
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adminconcessionsActions extends sfActions {

    /**
     * Executes concessions action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Concesión');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Concesión", "adminconcessions/index"));

        $q = Doctrine_Query::create()
                ->from('Concessions c')
                ->orderBy('c.name desc');

        $this->concessions = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
    }

    /**
     * Executes editConcessions action
     *
     * @param sfRequest $request A request object
     */
    public function executeEditConcessions(sfWebRequest $request) {

        $id = $this->getRequestParameter('id');

        $this->getResponse()->setSlot('title', 'Administración : Editar Concesión');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Concesión", "adminconcessions/index"), array("Editar Concesión", "adminconcessions/editConcessions?id=$id"));


        /** Concessions * */
        $q = Doctrine_Query::create()
                ->from('Concessions c')
                ->where('c.id_concession = ?', array($id))
                ->fetchArray();

        $this->concession = $q;
    }

    /**
     * Executes addConcessions action
     *
     * @param sfRequest $request A request object
     */
    public function executeAddConcessions(sfWebRequest $request) {

        $this->getResponse()->setSlot('title', 'Administración : Agregar Concesión');
        $this->breadcrumbs = array(array("Administración", "admin/index"), array("Concesión", "adminconcessions/index"), array("Agregar Concesión", "adminconcessions/addConcessions"));
    }

    /**
     * Executes createConcession action
     *
     * @param sfRequest $request A request object
     */
    public function executeCreateConcession(sfWebRequest $request) {


        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('concessions');

        try {

            $tabla = Doctrine::getTable('Concessions');
            $tabla = new Concessions();

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }


        $this->redirect('/adminconcessions/index');
    }

    /**
     * Executes updateConcession action
     * 
     * @param sfWebRequest $request
     * @return type 
     */
    public function executeUpdateConcession(sfWebRequest $request) {

        // Obtengo Datos del Formulario
        $_arrayPost = $request->getParameter('concessions');

        try {

            $tabla = Doctrine::getTable('Concessions')->find(array($_arrayPost["id"]));

            $tabla->name = strtoupper($_arrayPost['name']);

            $tabla->save();
        } catch (Exception $e) {
            syslog(LOG_ERR, "Error: " . $e->getMessage());
        }

        $this->redirect('/adminconcessions/index');
    }

    /**
     * CodeformatActive
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateActiveConcession(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $active = $request->getParameter('active');
        $active = (($active == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Concessions c')
                ->set('c.active', '?', $active)
                ->where('c.id_concession = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Concessions c')
                ->orderBy('c.name desc');

        $this->concessions = $q->fetchArray();


        $this->caps = $this->getUser()->getAttribute('caps');
        
        $this->setTemplate('lstConcessions');
    }

    /**
     * ConcessionDelete
     * [activo:inactivo]
     * @param sfWebRequest $request 
     */
    public function executeUpdateDeleteConcession(sfWebRequest $request) {

        // ID, Active
        $id = $request->getParameter('id');
        $deleted = $request->getParameter('deleted');
        $deleted = (($deleted == 0) ? 1 : 0);

        // Actualizo User
        $_data = Doctrine_Query::create()
                ->update('Concessions c')
                ->set('c.deleted', '?', $deleted)
                ->where('c.id_concession = ?', array($id))
                ->execute();

        // all Users
        $q = Doctrine_Query::create()
                ->from('Concessions c')
                ->orderBy('c.name desc');

        $this->concessions = $q->fetchArray();

        $this->caps = $this->getUser()->getAttribute('caps');
        $this->setTemplate('lstConcessions');
    }

}
